package com.test;

import java.awt.event.KeyEvent;
import java.util.LinkedList;

import com.input.GameAction;
import com.math3d.PointLight3d;
import com.math3d.Polygon3d;
import com.math3d.Rectangle3d;
import com.math3d.TexturePolygon3d;
import com.math3d.Vector3d;
import com.render2d.device.BaseScreenManager;
import com.render3d.ShadedTexturePolygonRender;
import com.render3d.texture.Texture;
import com.utils.test.BaseGameCore3d;
import com.utils.test.FastGameCore3d;

public class ShadedTextureRenderTest0 extends FastGameCore3d{
	public ShadedTextureRenderTest0() {
		// TODO Auto-generated constructor stub
		BaseScreenManager baseScreenManager = new BaseScreenManager(1024, 650, false);
		setScreenManager(baseScreenManager);
		run();
	}
	
	@Override
	protected void initPolygons() {
		// TODO Auto-generated method stub
		polygons = new LinkedList<Polygon3d>();
		TexturePolygon3d texurePolygon3d = new TexturePolygon3d(new Vector3d[]{new Vector3d(-200, 200, -500), new Vector3d(-400, 140, -500), new Vector3d(-180, 0, -500), new Vector3d(200, 0, -500), new Vector3d(100, 200, -500)});
		Texture texture = Texture.createTexture("resource/images/roof1s.png", true);
		setTexture(texture, texurePolygon3d);
		polygons.add(texurePolygon3d);
	}
	private void setTexture(Texture texture, Polygon3d polygon3d) {
		// TODO Auto-generated method stub
		if(polygon3d instanceof TexturePolygon3d){
			TexturePolygon3d texturePolygon3d = (TexturePolygon3d) polygon3d;
			Rectangle3d textureRect = new Rectangle3d();
			textureRect.getOrigin().setTo(texturePolygon3d.getVertex(0));
			textureRect.getDirectionV().setTo(texturePolygon3d.getVertex(1));
			textureRect.getDirectionV().subtract(textureRect.getOrigin());
			textureRect.getDirectionV().normalize();
			Vector3d u = new Vector3d();
			u.setToMultCrossVector3d(textureRect.getDirectionV(), texturePolygon3d.getNormal());
			u.normalize();
			textureRect.setDirectionU(u);
			textureRect.setWidth(texture.getWidth());
			textureRect.setHeight(texture.getHeight());
			texturePolygon3d.setTexture(texture, textureRect);
		}
	}
	
	@Override
	protected void initPolygonRender() {
		// TODO Auto-generated method stub
		super.initPolygonRender();
		ShadedTexturePolygonRender shadedTexturePolygonRender = new ShadedTexturePolygonRender(viewWindow, camera, true);
		shadedTexturePolygonRender.setAmbientLightIntensity(0.5f);
		pointLight3d = new PointLight3d();
		pointLight3d.setTo(-500, 500, 0);
		pointLight3d.lightIntensity = 1;
		pointLight3d.lightDistanceFalloff = 2000;
		shadedTexturePolygonRender.setPointLight(pointLight3d);
		polygonRender = shadedTexturePolygonRender;
		change = new GameAction("u", GameAction.DETECT_INIAL_PRESS_ONLY);
		inputManager.mapToKeyAction(KeyEvent.VK_U, change);
	}

	private PointLight3d pointLight3d;
	protected GameAction change;
	@Override
	protected void checkGameInput(long exTime) {
		// TODO Auto-generated method stub
		super.checkGameInput(exTime);
		if(change.isPressed()){
			pointLight3d.setChange();
		}
	}
	
	public static void main(String[] a){
		new ShadedTextureRenderTest0();
	}
}
